package com.ecco.persistent;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class ColourAgentBean implements ColourAgentLocal, ColourAgentRemote {
	
	//	 property constants
	public static final String COL_NAME = "col_name";
	public static final String AVAIL = "avail";

	@PersistenceContext(unitName="eccodb") private EntityManager entityManager;
	
	public void save(Colour persistentInstance) {
		entityManager.persist(persistentInstance);
	}
	
	public void delete(Colour persistentInstance) {
		entityManager.remove(persistentInstance);
	}
	
	public Colour update(Colour detachedInstance) {
		return entityManager.merge(detachedInstance);
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Colour> findByColName(String col_name) {
		String queryString = "SELECT model FROM Colour model WHERE model.col_name = :colNameValue";
		return entityManager.createQuery(queryString).setParameter(
				"colNameValue", col_name).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<Colour> searchByColName(String col_name) {
		String queryString = "SELECT model FROM Colour model WHERE model.col_name LIKE :colNameValue";
		return entityManager.createQuery(queryString).setParameter(
				"colNameValue", col_name).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<Colour> findByAvail(String avail) {
		String queryString = "SELECT model FROM Colour model WHERE model.avail = :colAvailValue";
		return entityManager.createQuery(queryString).setParameter(
				"colAvailValue", avail).getResultList();
	}
	
	public Colour findColourById(Integer col_id) {
		Colour colour = entityManager.find(Colour.class, col_id);
		colour.getAocRecords().size();
		return colour;
	}
	
	@SuppressWarnings("unchecked")
	public List<Colour> findAll() {
		String queryString = "SELECT model FROM Colour model";
		return entityManager.createQuery(queryString).getResultList();
	}	
	
	public void addColour(String col_name, String avail) {
		Colour colour = new Colour();
		colour.setCol_name(col_name);
		colour.setAvail(avail);
		entityManager.persist(colour);
	}
	
	public void updateColour(Integer col_id, String col_name, String avail) {
		Colour colour = entityManager.find(Colour.class, col_id);
		if (colour!=null) {
			colour.setCol_name(col_name);
			colour.setAvail(avail);
			entityManager.merge(colour);
		}
	}
		
	public void removeColourById(Integer col_id) {
		Colour colour = entityManager.find(Colour.class, col_id);
		if (colour!=null) entityManager.remove(colour);
	}
	
	public Integer findNumOfAOCRecordsByCol_id(Integer col_id) {
		Colour colour = entityManager.find(Colour.class, col_id);
		return colour.getAocRecords().size();
	}
		
	@SuppressWarnings("unchecked")
	public Boolean checkByColName(String col_name) {
		String queryString = "SELECT model FROM Colour model WHERE model.col_name = :colNameValue";
		List checkColourResult = entityManager.createQuery(queryString).setParameter(
				"colNameValue", col_name).getResultList();
		if (checkColourResult.size()== 0){return false;}
			return true;
	}
	
}
